package com.exartisan.fragment;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ProgressBar;

import com.actionbarsherlock.app.SherlockFragment;
import com.exartisan.adapter.HomeGridAdapter;
import com.exartisan.bean.Event;
import com.exartisan.network.NetworkUtility;
import com.exartisan.network.RestConnector;
import com.exartisan.network.StupidResponseTranslater;
import com.exartisan.network.WeirdParametersBuilder;
import com.exartisan.yn_event.Event_Feeds;
import com.exartisan.yn_event.R;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class HomeFragment extends SherlockFragment {
private GridView eventGrid;
private ProgressBar prBar;
private Button btnRetry;
private ArrayList<Event> events;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View root = inflater.inflate(R.layout.home_fragment, container, false);
		eventGrid = (GridView) root.findViewById(R.id.event_grid);
		prBar = (ProgressBar) root.findViewById(R.id.progress_bar);
		btnRetry = (Button) root.findViewById(R.id.btn_retry);
		btnRetry.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				doNetworkTask();
			}
		});
		if (events == null) doNetworkTask();
		else
			setUpGridView(events);
		
		
		return root;
	}
	
	private void setUpGridView(ArrayList<Event> events){
		prBar.setVisibility(View.GONE);
		btnRetry.setVisibility(View.GONE);
		eventGrid.setVisibility(View.VISIBLE);
		eventGrid.setAdapter(new HomeGridAdapter(getSherlockActivity(), events));
		eventGrid.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				Intent i = new Intent(getSherlockActivity(), Event_Feeds.class);
				i.putExtra("event", HomeFragment.this.events.get(pos));
				startActivity(i);
			}
		});
	}
	
	private void doNetworkTask(){
		if(NetworkUtility.checkNetworkState(getSherlockActivity())){
			eventGrid.setVisibility(View.GONE);
			prBar.setVisibility(View.VISIBLE);
			btnRetry.setVisibility(View.GONE);
			RestConnector.post("", WeirdParametersBuilder.BuildParams(WeirdParametersBuilder.BuildTopEventData()), new AsyncHttpResponseHandler(){
				@Override
				public void onSuccess(int arg0, String response) {
					prBar.setVisibility(View.GONE);
					//Log.d("top_event_response", response.toString());
					if (response == null) {
						btnRetry.setVisibility(View.VISIBLE);
						return;
					}
					if (StupidResponseTranslater.checkSuccess(response)){
					events = StupidResponseTranslater.getTopEvent(response);
					setUpGridView(events);
					} else 
						btnRetry.setVisibility(View.VISIBLE);
					super.onSuccess(arg0, response);
				}
				
				@Override
				public void onFailure(Throwable arg0, String arg1) {
					prBar.setVisibility(View.GONE);
					btnRetry.setVisibility(View.VISIBLE);
					super.onFailure(arg0, arg1);
				}
			});
		}
		
		else {
			NetworkUtility.showNoNetworkDialog(getSherlockActivity());
			btnRetry.setVisibility(View.VISIBLE);
		}
	}

}
